package com.care.dao.doctor;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.care.entity.Admin;
import com.care.entity.Doctor;
import com.care.entity.User;
import com.care.ui.DocotorPage;

/**
 * 
 * @author jiangxiongjie
 *	2020年3月9日 下午5:21:25
 */
@Repository
public class DoctorDaoImpl  {
	@Resource
	private SessionFactory sessionFactory;
	public Session getSession(){
		return this.sessionFactory.getCurrentSession();
	}
	/**
	 * 医生注册功能
	 */
	public void DoctorRegister(Doctor doctor){
		doctor.setEmail(doctor.getEmail());
		doctor.setImg(doctor.getImg());
		doctor.setName(doctor.getName());
		doctor.setPassword(doctor.getPassword());
		doctor.setSection(doctor.getSection());
		doctor.setSex(doctor.getSex());		
		getSession().save(doctor);		
	}
	/**
	 * 是否已经被注册
	 */	
	public boolean RegisterUnique(String email){
		String hql="from Doctor where email=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, email);
		Doctor doctor=(Doctor) query.uniqueResult();
		if (doctor == null) {
			return true;
		}				
		return false;
		
	}
	
	/**
	 * 根据邮箱查询医生全部信息
	 * @param email
	 * @return
	 */
	public Doctor FindDoctorByEmail(String email){
		String hql="from Doctor where email=? ";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, email);		
		Doctor doctor=(Doctor) query.uniqueResult();	    
		return doctor;
	
	}
	
	/**
	 * 医生登录
	 * @param doctor
	 * @return
	 */
	public Boolean DoctorLogin(Doctor doctor){
		String hql="from Doctor where email=? and password=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, doctor.getEmail());
		query.setParameter(1, doctor.getPassword());
		doctor=(Doctor) query.uniqueResult();
		
		if (doctor!=null) {
			return true;
		}
		return false;		
	}
	
	/**
	 * 管理员登录
	 */
	public Boolean AdminLogin(Admin admin){
		String hql="from Admin where email=? and password=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, admin.getEmail());
		query.setParameter(1, admin.getPassword());
		admin=(Admin) query.uniqueResult();
		if (admin != null) {
			return true;
		}
		return false;				
	}
	/**
	 * 根据邮箱查询管理员全部信息
	 */
	public Admin FindAdminByEmail(String email){
		String hql="from Admin where email=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, email);
		Admin admin=(Admin) query.uniqueResult();		
		return admin;
		
	}
	/**
	 * 用户登录
	 */
	public Boolean UserLogin(User user){
		String hql="from User where email=? and password=?";		
		Query query=getSession().createQuery(hql);
		query.setParameter(0, user.getEmail());
		query.setParameter(1, user.getPassword());
		user=(User) query.uniqueResult();
		if (user !=null) {
			return true;
		}
		return false;
	}
	/**
	 * 根据邮箱查询用户全部信息
	 */
	public User FindUserByUser(String email){
		String hql="from User where email=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, email);
		User user=(User) query.uniqueResult();
		return user;
	}
	/**
	 * 分页
	 * @param docotorPage
	 * @return
	 */
	public List<Doctor> FindDoctorByPage(int pageNum,int pageSize){		
		String hql="from Doctor ";
		Query query=getSession().createQuery(hql);		
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);			
		return query.list();		
	}
	/**
	 * 记录总条数
	 * @return
	 */
	public int CountDoctor(){
		String hql="select count(*) from  Doctor";
		Query query=getSession().createQuery(hql);
		int count=((Long) query.setCacheable(true).uniqueResult()).intValue();//此处用Long类型进行转换		
		return count;		
	}
	/**
	 * 科室分页
	 * @param pageNum
	 * @param pageSize
	 * @param section
	 * @return
	 */
	public List<Doctor> FindDoctorBySection(int pageNum,int pageSize,String section){
		String hql="from Doctor where section=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, section);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		return query.list();	
	}
	/**
	 * 根据部门计算总数
	 * @param section
	 * @return
	 */
	public int CountSectionDoctor(String section){
		String hql="select count(*) from Doctor where section=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, section);
		int count=((Long)query.setCacheable(true).uniqueResult()).intValue();
		return count;		
	}
	/**
	 * 查询并显示到文案上
	 * @param doctor
	 * @return
	 */
	public Doctor findDoctor(String email){
		String hql="from Doctor where email=?";
		Query query=getSession().createQuery(hql);
		query.setParameter(0, email);
		Doctor doctor=(Doctor) query.uniqueResult();
		return doctor;
		
	}
	/**
	 * 修改医生个人信息
	 * @param doctor
	 * @return 
	 */
	public void updateDoctor(Doctor doctor,String img,String information,String name,String sex,String section,String password){					
		doctor.setInformation(information);	
		doctor.setImg(img);		
		doctor.setName(name);			
		doctor.setSex(sex);
		doctor.setSection(section);
		doctor.setPassword(password);		
		getSession().saveOrUpdate(doctor);
	}
	/**
	 * 充值金额
	 * @param email
	 * @param money
	 */
    public void updateMoney(String email,double money){
    	String hql="select money from Doctor where email=?";
    	this.getSession().createQuery(hql).setParameter(0, email).uniqueResult();
    	String hql1="update Doctor set money=money+? where email=?";
    	this.getSession().createQuery(hql1).setParameter(0, money).setParameter(1, email).executeUpdate();  	
    }
    
}
